home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / collation / demos-1.1 / TabPanel.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  8.3 KB  |  273 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /* ************************************************************* */
  18. /* (C) Copyright Taligent, Inc. 1996-All Rights Reserved.        */
  19. /* (C) Copyright IBM Corporation 1996                            */
  20. /*                                                               */
  21. /* Permission is granted to copy, use, modify, and merge this    */
  22. /* software into your applications  and to permit others to do      */
  23. /* any of the foregoing. You must include this permission and      */
  24. /* copyright notice in all copies and modified versions of this  */
  25. /* software, and include attribution to Taligent in all splash      */
  26. /* screens included in any application using this software.      */
  27. /* THIS SOFTWARE IS PROVIDED IN ITS 'AS IS' CONDITION. TALIGENT  */
  28. /* DISCLAIMS ANY LIABILITY OF ANY KIND FOR DAMAGES WHATSOEVER      */
  29. /* RESULTING FROM THE USE OF THIS SOFTWARE.                         */
  30. /* ************************************************************* */
  31.  
  32. import java.awt.*;
  33. import java.util.Vector;
  34.  
  35. /**
  36.  * Tabbed panel. Displays a series of cards that are selected
  37.  * by clicking on the various tabs displayed on the panel.
  38.  * Tabs can be arranged on the top, bottom, or either side of
  39.  * the tabbed panel.
  40.  *
  41.  * @author  Andy Clark, Taligent Inc.
  42.  * @version 1.0
  43.  */
  44. public class TabPanel
  45.     extends Panel
  46.     {
  47.     // Constants
  48.     final static int TOP    = 0;
  49.     final static int BOTTOM = 1;
  50.     final static int LEFT   = 2;
  51.     final static int RIGHT  = 3;
  52.  
  53.     final static int DEFAULT_PLACEMENT = TOP;
  54.  
  55.     // Data
  56.     TabCanvas tabcanvas;
  57.     BorderPanel display;
  58.     int style = TOP;
  59.     Font font;
  60.  
  61.     /**
  62.      * Constructor.
  63.      */
  64.     public TabPanel() {
  65.  
  66.         // setup display
  67.         setLayout(new BorderLayout());
  68.         display = new BorderPanel(BorderPanel.RAISED, 2);
  69.         display.setLayout(new CardLayout());
  70.  
  71.         super.add("Center", display);
  72.         tabcanvas = new TabCanvas(display);
  73.  
  74.         setPlacement(DEFAULT_PLACEMENT);
  75.         }
  76.  
  77.     public boolean didCardChange() {
  78.         return tabcanvas.cardChanged;
  79.     }
  80.  
  81.     /**
  82.      * Sets the placement of the tabs.
  83.      *
  84.      * @param placement Where to place the tabs.
  85.      */
  86.     public TabPanel setPlacement(int placement) {
  87.  
  88.         // remove old canvas from layout and then readd in new position
  89.         remove(tabcanvas);
  90.         switch (placement) {
  91.             case BOTTOM: super.add("South", tabcanvas); break;
  92.             case LEFT: super.add("West", tabcanvas); break;
  93.             case RIGHT: super.add("East", tabcanvas); break;
  94.             case TOP:
  95.             default: // assume top if invalid
  96.                 super.add("North", tabcanvas); break;
  97.             }
  98.  
  99.         return this;
  100.         }
  101.  
  102.     /**
  103.      * Add a panel to the deck of tabbed panels.
  104.      *
  105.      * @param label The tab label
  106.      * @param comp  The component to add.
  107.      */
  108.     public Component add(String label, Component comp) {
  109.  
  110.         tabcanvas.add(label, comp);
  111.         return display.add(label, comp);
  112.         }
  113.  
  114.     /**
  115.      * Paints the panel. First paints the raised border that we
  116.      * inherited and then paints in the tabs.
  117.      */
  118.     public void paint(Graphics g) {
  119.  
  120.         // draw border
  121.         super.paint(g);
  122.  
  123.         // draw tabs
  124.         }
  125.  
  126.     }
  127.  
  128. class TabCanvas
  129.     extends Canvas
  130.     {
  131.     public BorderPanel display;
  132.     public boolean cardChanged = false;
  133.  
  134.     public Vector tabs = null;
  135.  
  136.     public int selected = -1;
  137.     /**
  138.      * Constructor.
  139.      *
  140.      * @param display   The display panel.
  141.      */
  142.     public TabCanvas(BorderPanel display) {
  143.  
  144.         // init data
  145.         this.display = display;
  146.         tabs = new Vector();
  147.         }
  148.  
  149.     /**
  150.      * Returns the minimum size of the tabs.
  151.      */
  152.     public Dimension getPreferredSize() { return getMinimumSize(); }
  153.     public Dimension getMinimumSize() {
  154.  
  155.         return new Dimension(0, getGraphics().getFontMetrics().getHeight() + 4 * display.getThickness());
  156.         }
  157.  
  158.     /**
  159.      * Adds a tab.
  160.      *
  161.      * @param name  The tab caption.
  162.      * @param comp  The component this tab is connected to.
  163.      */
  164.     public void add(String name, Component comp) {
  165.         Tab tab = new Tab();
  166.         tab.name = name;
  167.         tab.comp = comp;
  168.         tab.selected = tabs.size() < 1;
  169.  
  170.         tabs.addElement(tab);
  171.         }
  172.  
  173.     /**
  174.      * Paint the tabs.
  175.      *
  176.      * @param g The graphics context.
  177.      */
  178.     public void paint(Graphics g) {
  179.  
  180.         // calculate size, colors, etc.
  181.         Dimension size = getSize();
  182.         Color light = getBackground().brighter().brighter().brighter();
  183.         Color dark = getBackground().darker().darker().darker();
  184.         int thickness = display.getThickness();
  185.         int offset = 5;
  186.  
  187.         //
  188.         // Draw tabs
  189.         //
  190.  
  191.         // redraw upper border
  192.         Graphics displayg = display.getGraphics();
  193.         displayg.setColor(light);
  194.         for (int i = 0; i < thickness; i++)
  195.             displayg.drawLine(i, i, size.width - i - 1, i);
  196.  
  197.         // set font
  198.         Font oldfont = g.getFont();
  199.         oldfont = new Font(oldfont.getName(),
  200.                            oldfont.getStyle() & ~Font.BOLD,
  201.                            oldfont.getSize());
  202.         Font font = new Font(oldfont.getName(), oldfont.getStyle() | Font.BOLD, oldfont.getSize());
  203.         FontMetrics fm;
  204.  
  205.         // draw each tab
  206.         for (int index = 0; index < tabs.size(); index++) {
  207.             Tab tab = (Tab)tabs.elementAt(index);
  208.  
  209.             g.setFont(oldfont);
  210.             fm = g.getFontMetrics(oldfont);
  211.             int strwidth = fm.stringWidth(tab.name);
  212.             if (tab.selected) {
  213.                 g.setFont(font);
  214.                 fm = g.getFontMetrics(font);
  215.                 strwidth = fm.stringWidth(tab.name);
  216.                 displayg.setColor(getBackground());
  217.                 displayg.fillRect(offset + thickness, 0, strwidth + 10 - 2 * thickness + 1, thickness);
  218.                 }
  219.             int adjustment = tab.selected ? 0 : thickness;
  220.             for (int i = 0; i < thickness; i++) {
  221.                 g.setColor(light);
  222.                 g.drawLine(offset + i, i + adjustment, offset + strwidth + 10 - i, i + adjustment);
  223.                 g.drawLine(offset + i, i + 1, offset + i, size.height - 1);
  224.                 g.setColor(dark);
  225.                 g.drawLine(offset - i + strwidth + 10, i + 1, offset - i + strwidth + 10, size.height - 1);
  226.                 }
  227.             displayg.setColor(dark);
  228.             g.setColor(Color.black);
  229.             g.drawString(tab.name, offset + 5, thickness + adjustment + fm.getAscent());
  230.  
  231.             tab.bounds = new Rectangle(offset, 0, strwidth + 10, size.height);
  232.  
  233.             offset += strwidth + 10 + thickness;
  234.             }
  235.  
  236.         }
  237.  
  238.     /**
  239.      * Event handler.
  240.      *
  241.      * @param evt   The event that occured.
  242.      */
  243.     public boolean handleEvent(Event evt) {
  244.  
  245.         // see where they clicked
  246.         if (evt.id == Event.MOUSE_UP) {
  247.             for (int index = 0; index < tabs.size(); index++) {
  248.                 Tab tab = (Tab)tabs.elementAt(index);
  249.                 if (tab.bounds.contains(evt.x, evt.y)) {
  250.                     for (int i = 0; i < tabs.size(); i++)
  251.                         ((Tab)tabs.elementAt(i)).selected = false;
  252.                     tab.selected = true;
  253.                     ((CardLayout)display.getLayout()).show(display, tab.name);
  254.                     repaint();
  255.                     cardChanged = true;
  256.                     }
  257.                 }
  258.             }else if(cardChanged == true) cardChanged = false;
  259.  
  260.         return super.handleEvent(evt);
  261.         }
  262.  
  263.     }
  264.  
  265. class Tab
  266.     extends Object
  267.     {
  268.     public Component comp = null;
  269.     public String name = "";
  270.     public boolean selected = false;
  271.     public Rectangle bounds = new Rectangle();
  272.     }
  273.